﻿using CSharpDay11.Adapters.Interfaces;
using CSharpDay11.Data;
using CSharpDay11.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity; // used for GetRoles()

namespace CSharpDay11.Adapters.Adapters
{
    public class UserAdapter : IUserAdapter
    {
        public LoggedInUserViewModel GetUser(string Id, ApplicationUserManager manager)
        {
            LoggedInUserViewModel model = new LoggedInUserViewModel();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model.Username = db.Users.Where(x => x.Id == Id).Select(x => x.UserName).FirstOrDefault();
            }

            var Roles = manager.GetRoles(Id);
            if (Roles.Contains("Basic User")) model.RoleName = "Basic User";
            else if (Roles.Contains("Admin")) model.RoleName = "Admin";
            else model.RoleName = "N/A";

            return model;
        }
        
    }
}